import 'dart:convert';

import 'package:app/common/tool.dart';
import 'package:app/utils/persisten_storage.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../common/colors.dart';
import '../../components/base_scaffold_appbar.dart';
import '../../services/address.dart';

class SetUrlPage extends StatefulWidget {

  SetUrlPage({Key? key, required this.position}) : super(key: key) {

  }

  var position = -1;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SetUrlPageState();
  }
}

class SetUrlPageState extends State<SetUrlPage> {
  /// http://122.51.164.176:8072
  var urltf = TextEditingController();
  Map servemap = {};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    syncInit();
  }

  void syncInit() async {
    var prefs = await SharedPreferences.getInstance();
    var urlJson = await PersistentStorage().hasKey('urlJson');
    if (urlJson == true) {
      String urlString = prefs.getString('urlJson')!;
      servemap = json.decode(urlString);
      urltf.text = servemap[widget.position.toString()];
    } else {
      urltf.text = '${Address.homeHost}';
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('设置URL'),
        action: [
              TextButton(
                  onPressed: () async {
                    if (urltf.text == 'http://') {
                      // Toast.toast(context,msg: '请输入正确的url');
                      BotToast.showText(text: '请输入正确的url');
                      return;
                    }
                    var prefs = await SharedPreferences.getInstance();
                    if(widget.position==-1){
                      servemap[servemap.length.toString()] = urltf.text;
                    }else{
                      servemap[widget.position.toString()] = urltf.text;
                    }
                    print("urlJson==>>>${json.encode(servemap)}");

                    prefs.setString('urlJson', json.encode(servemap));
                    Get.back(result: 'refresh');
                    setState(() {});

                  },
                  child: const Text(
                    '保存',
                    style: TextStyle(color: Colors.white),
                  )),
        ],
      ),
      // AppBar(
      //   backgroundColor: AppColor.themeColor,
      //   title: Text('设置URL'),
      //   leading: IconButton(
      //     onPressed: () {
      //       Get.back();
      //     },
      //     icon: Image.asset(
      //       'images/leftarr.png',
      //       width: 15,
      //       height: 15,
      //       color: Colors.white,
      //     ),
      //   ),
      //   actions: [
      //     TextButton(
      //         onPressed: () async {
      //           if (urltf.text == 'http://') {
      //             // Toast.toast(context,msg: '请输入正确的url');
      //             BotToast.showText(text: '请输入正确的url');
      //             return;
      //           }
      //           var prefs = await SharedPreferences.getInstance();
      //           if(widget.position==-1){
      //             servemap[servemap.length.toString()] = urltf.text;
      //           }else{
      //             servemap[widget.position.toString()] = urltf.text;
      //           }
      //           print("urlJson==>>>${json.encode(servemap)}");
      //
      //           prefs.setString('urlJson', json.encode(servemap));
      //           Get.back(result: 'refresh');
      //           setState(() {});
      //
      //         },
      //         child: const Text(
      //           '保存',
      //           style: TextStyle(color: Colors.white),
      //         )),
      //   ],
      // ),
      body: Column(
        children: [
          Center(
            child: Container(
              margin: EdgeInsets.only(top: 20),
              padding: EdgeInsets.only(left: 10),
              decoration: BoxDecoration(
                //背景
                color: Theme.of(context).cardColor,
                //设置四周圆角 角度
                borderRadius: BorderRadius.all(Radius.circular(4.0)),
                //设置四周边框
                border: Border.all(width: 1, color: AppColor.lineColor),
              ),
              width: Get.width,
              child: TextField(
                onChanged: (v) {},
                decoration: InputDecoration(border: InputBorder.none),
                controller: urltf,
                keyboardType: TextInputType.url,
                // textInputAction: TextInputAction.none,
              ),
            ),
          )
        ],
      ),
    );
  }
}
